package puyu.manage.service.security.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import puyu.manage.service.SysUser.dao.SysUserMapper;
import puyu.manage.service.constant.AjaxResult;
import puyu.manage.service.constant.Constants;
import puyu.manage.service.role.dao.SysRoleMapper;
import puyu.manage.service.security.LoginUser;
import puyu.manage.service.security.SecurityUtils;
import puyu.manage.service.utils.MessageUtils;
import puyu.manage.service.utils.StringUtils;
import puyu.manage.service.utils.except.CustomException;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录校验方法
 *
 * @author cxnet
 */
@Component
public class SysLoginService {
    @Autowired(required = false)
    private TokenService tokenService;

    @Autowired(required = false)
    private SysUserMapper sysUserMapper;

    @Autowired(required = false)
    private SysRoleMapper sysRoleMapper;


    /**
     * sso登录验证
     *
     * @param username 用户名
     * @return 结果
     */
    public String loginBySso(String username) {
        LoginUser loginUser = new LoginUser();
        Map<String,Object> sysUser = sysUserMapper.selectUserInfoByUserName(username);
        if (sysUser == null) {
            throw new CustomException(MessageUtils.message("A0100",username));
        }
        // 角色信息
        List<Map<String,Object>> sysRoles = sysRoleMapper.selectRolesByUserName(sysUser.get("userName")+"");
        sysUser.put("roles",sysRoles);
        loginUser.setUser(sysUser);
        // 生成token
        return tokenService.createToken(loginUser, Constants.WEB);
    }

    /**
     * 开启cas后登录验证
     *
     */
    public AjaxResult loginByCas(Map<String,Object> params) {
        AjaxResult ajax = AjaxResult.success();
        String userName = params.get("userName")+"";
        String passWord = params.get("password")+"";
        Map<String,Object> sysUser = sysUserMapper.selectUserInfoByUserName(userName);
        if (sysUser == null) {
            throw new CustomException(MessageUtils.message("A0121"));
        }
        boolean b = SecurityUtils.matchesPassword(passWord, sysUser.get("password")+"");
        if (!b) {
            throw new CustomException(MessageUtils.message("A0121"));
        }
        responseValue(this.loginBySso(userName), "", ajax);
        return ajax;
    }

    /**
     * 响应数据
     *
     * @param token
     * @param type
     * @param ajax
     */
    public void responseValue(String token, String type, AjaxResult ajax) {
        ajax.put(Constants.TOKEN, token);
        if (Constants.SHOW_CHECK.equals(token)) {
            ajax.put(Constants.CODE, 200);
            ajax.put(Constants.MSG, MessageUtils.message("A0124"));
        }
        // 验证密码复杂度
        if (Constants.SUCCESS.equals(token)) {
            ajax.put("data", "");
            ajax.put(Constants.CODE, 200);
            ajax.put(Constants.MSG, MessageUtils.message("A0122"));
        }
        // 密码是否过期
        if (StringUtils.isNotEmpty(type)) {
            ajax.put("data", "");
            ajax.put(Constants.MSG, MessageUtils.message("A0123"));
            ajax.put("type", type);
        } else {
            ajax.put("type", "");
        }
    }
}
